<!DOCTYPE html>
<html lang="zh-cn">
<head>
  
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>
高亮Markdown中的代码（续） |
穷折腾</title>
  <link rel="stylesheet" href="/static/css/style.css" />
  <link rel="stylesheet" href="/static/css/pygments.css" />
  <link rel="alternate" type="application/rss+xml" title="RSS" href="http://blog.zorro.im/rss.xml" />
  <link rel="icon" type="image/png" href="/static/img/favicon.png">
</head>
<body>
    <div class="page-wrap">
        <section class="main-header regular">
            <div class="vertical-container">
                <div class="wrapper clearfix">
                    <header>
                        <a href="/">
                            <h1 id="main-title">穷折腾</h1>
                            <h2 id="main-subtitle">zqqf16 的个人博客</h2>
                        </a>
                    </header>
                    <nav id="main-nav" role="navigation">
                        <a href="/" class="selected">主页</a>
                        <a href="/posts/about.html" class="">关于</a>
                        <a href="https://github.com/zqqf16">Github</a>
                    </nav>
                </div>
            </div>
        </section>
        <section class="main-content">
            
<article class="main-content">
    <div class="wrapper">
        <header class="section-header">
            <h1 class="section-title">高亮Markdown中的代码（续）</h1>
            <span class="section-subtitle"><time datetime="2013-08-20" pubdate="">2013-08-20</time></span>
        </header>
        <section class="post-content">
        	<p>之前写过一篇文章（<a href="/posts/how-to-highlight-code-in-markdown.html">高亮Markdown中的代码</a>)，介绍了python-markdown2中高亮代码的方法。后来在使用中我发现了一个<code>fenced code blocks</code>的bug，如果代码中间有空行，它会把换行之后的部分当成嵌套的。生成的文件中会有一堆类似这样的部分：</p>
<div class="codehilite"><pre><span class="nt">&lt;span</span> <span class="na">class=</span><span class="s">&quot;k&quot;</span><span class="nt">&gt;</span>class<span class="nt">&lt;/span&gt;</span> <span class="nt">&lt;span</span> <span class="na">class=</span><span class="s">&quot;nc&quot;</span><span class="nt">&gt;</span>
</pre></div>


<p>后来到其Github上一搜，发现很多人都有类似问题，比如<a href="https://github.com/trentm/python-markdown2/issues/109">这里</a>。也有人给出了<a href="https://github.com/trentm/python-markdown2/pull/117">解决方案</a>。</p>
<p>但是这个Pull request已经提出4个月了，原作者至今没有通过，而且整个代码也有近1年没有动过了。Clone了一份它的代码，发现如果想自定义扩展的话比较困难。无奈之下从新研究起了曾经被我抛弃的Python-Markdown。结果发现原来它也有很多扩展，而且比Markdown2中的更多。更重要的是提供了扩展的接口，很方便地写自己的扩展。于是就在peanut中替换了markdown2，代码如下：</p>
<div class="codehilite"><pre><span class="n">md</span> <span class="o">=</span> <span class="n">markdown</span><span class="o">.</span><span class="n">Markdown</span><span class="p">(</span><span class="n">extensions</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;fenced_code&#39;</span><span class="p">,</span> <span class="s">&#39;codehilite&#39;</span><span class="p">,</span> <span class="s">&#39;meta&#39;</span><span class="p">])</span>
<span class="c">#do something</span>
<span class="n">html</span> <span class="o">=</span> <span class="n">md</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="n">content</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s">&#39; </span><span class="se">\n</span><span class="s">&#39;</span><span class="p">))</span>
</pre></div>


<p><code>fenced_code</code>用来识别</p>
<div class="codehilite"><pre>```python
```
</pre></div>


<p>这样的代码段，<code>codehilite</code>用来高亮代码，<code>meta</code>用来识别文章信息，比如：</p>
<div class="codehilite"><pre>title: 高亮Markdown中的代码（续）
date: 2013-8-20
tag: python
     markdown
</pre></div>


<p>过度完毕，问题搞定~</p>
        </section>
        <div class="post-nav">
    		<!-- Duoshuo Comment BEGIN -->
<div class="ds-thread"></div>
<script type="text/javascript">
	var duoshuoQuery = {short_name:"zqqf16"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = 'http://static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0]
		|| document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
</script>
<!-- Duoshuo Comment END -->

    	</div>
    </div>

</article>

        </section>
    </div>
    <footer class="main-footer">
        <div class="vertical-container">
            <div class="wrapper">
                <p class="copy">&copy; zqqf16</p>
            </div>
        </div>
    </footer>
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-41282906-2', 'auto');
      ga('require', 'displayfeatures');
      ga('send', 'pageview');
    </script>
</body>
</html>
